/**
 * 
 */
package controller;

import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

import com.actionbarsherlock.app.SherlockFragment;
import com.example.remotecontrol.R;
import components.AlarmManager;
import components.FragmentMediator;
import components.SMSCommandManager;
import components.SMSSendingCommandFactory;
import components.SoundManager;

import data.SMSCommand;

/**
 * @author User
 * 
 */
public class HomeFragment extends SherlockFragment implements
		View.OnClickListener {

	private Button btnAlert;
	private Button btnDevice;
	private Button btnAlertOption;
	private Button btnDeviceOption;
	private Button btnUpdateAnimation;
	private Button btnUpdate;
	private MediaPlayer mediaPlayer;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.home, container, false);

		view.findViewById(R.id.btn_power).setOnClickListener(this);
		btnAlert = (Button) view.findViewById(R.id.btn_home_alarm);
		//btnAlert.setOnClickListener(this);
		btnDevice = (Button) view.findViewById(R.id.btn_power);
		btnAlertOption = (Button) view.findViewById(R.id.btn_home_alarm_option);
		btnAlertOption.setOnClickListener(this);
		btnDeviceOption = (Button) view
				.findViewById(R.id.btn_home_device_option);
		btnDeviceOption.setOnClickListener(this);
		btnUpdateAnimation = (Button) view.findViewById(R.id.btn_home_update);
		btnUpdateAnimation.setOnClickListener(this);
		btnUpdate = (Button) view.findViewById(R.id.btn_update);
		btnUpdate.setOnClickListener(this);
		
		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		SoundManager.sharedSoundManager().playClickButtonSound(getActivity());
		switch (v.getId()) {
		case R.id.btn_power:

			onClickPowerButton();
			break;
		case R.id.btn_home_alarm_option:
			onClickAlarmOption();
			break;
		case R.id.btn_home_device_option:
			onClickHomeDeviceOption();
			break;
//		case R.id.btn_home_alarm:
//			if (timerAlarmUpdate == null) {
//				timerAlarmUpdate = new Timer();
//				TimerTask task = new TimerTask() {
//
//					@Override
//					public void run() {
//						// TODO When can not recieve update sms
////						timerUpdate.cancel();
////						timerUpdate = null;
//						timerAlarmUpdate.cancel();
//						timerAlarmUpdate = null;
//						stopAlertAnimation();
//						
//					}
//				};
//
//				timerAlarmUpdate.schedule(task, 1 * 1000);
//				startAlertAnimation();
//
//			}
//			break;
		case R.id.btn_home_update:
		case R.id.btn_update:
		{
			onClickHomeUpdate();

			}
			// break;
		default:
			break;
		}
	}

	/**
	 * 
	 */
	private void onClickHomeUpdate() {
		SMSCommand smsCommand = SMSSendingCommandFactory.createSMSCommandWithConfiguredPhone(getActivity(),
				SMSCommand.CMD_CODE_UPDATE, "");
		SMSCommandManager.sendSMSMessage(getActivity(), smsCommand);
		
		AlarmManager.sharedAlarmList(getActivity()).setIsUpdating(true);
//			if (timerRefreshUpdate == null) {
//				timerRefreshUpdate = new Timer();
//				TimerTask task = new TimerTask() {
//
//					@Override
//					public void run() {// TODO When can not recieve update sms
//						timerRefreshUpdate.cancel();
////						timerUpdate.cancel();
//						timerRefreshUpdate = null;
//						stopUpdateAnimation();
//						
//					}
//				};
//
//				timerRefreshUpdate.schedule(task, 3 * 1000);
			startUpdateAnimation();
	}

	/**
	 * 
	 */
	private void onClickHomeDeviceOption() {
		Fragment deviceOption = FragmentMediator
				.CreateFragment(FragmentMediator.FRAGMENT_DEVICE_OPTION);
		FragmentMediator.transitView(this, deviceOption, null, true);
	}

	/**
	 * 
	 */
	private void onClickAlarmOption() {
		Fragment alarmOption = FragmentMediator
				.CreateFragment(FragmentMediator.FRAGMENT_ALARM_OPTION);
		FragmentMediator.transitView(this, alarmOption, null, true);
	}

	/**
	 * 
	 */
	private void onClickPowerButton() {
		SMSCommand smsMessage = SMSSendingCommandFactory
				.createSMSCommandWithConfiguredPhone(getActivity(),
						SMSCommand.CMD_CODE_POWER, "");
		SMSCommandManager.sendMessageWithDialog(this.getActivity(),
				smsMessage);
	}

	private void startUpdateAnimation() {
		btnUpdateAnimation.setBackgroundResource(R.drawable.updating);
		Animation rotation = AnimationUtils.loadAnimation(this.getActivity(),
				R.anim.update_rotate);
		rotation.setRepeatCount(Animation.INFINITE);
		btnUpdateAnimation.startAnimation(rotation);
	}

	private void stopUpdateAnimation() {
		if (btnUpdateAnimation.getAnimation() != null) {
			btnUpdateAnimation.getAnimation().cancel();
		}

		btnUpdateAnimation.post(new Runnable() {
			
			@Override
			public void run() {
				btnUpdateAnimation.setBackgroundResource(R.drawable.update_stop);
			}
		});
		
		
	}

	private void startAlertAnimation() {
		
		SoundManager.sharedSoundManager().playAlertSound(getActivity());
		
		btnAlert.setBackgroundResource(R.drawable.alert_alarm);
		AnimationDrawable anim = (AnimationDrawable) btnAlert.getBackground();

		if (!anim.isRunning()) {
			anim.start();
		}
		// Animation rotation = AnimationUtils.loadAnimation(this.getActivity(),
		// R.anim.update_rotate);
		// rotation.setRepeatCount(Animation.INFINITE);
		// btnUpdate.startAnimation(rotation);
	}

	private void stopAlertAnimation() {
		SoundManager.sharedSoundManager().stopAlertSound();
//		if (btnAlert.getAnimation() != null) {
//			btnAlert.getAnimation().cancel();
//		}
		
		
		
		if (btnAlert.getBackground()  instanceof AnimationDrawable) {
			AnimationDrawable anim = (AnimationDrawable) btnAlert.getBackground() ;
					if (anim.isRunning()) {
						
						anim.stop();
						
						btnAlert.post(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								btnAlert.setBackgroundResource(R.drawable.bell_normal);
							}
						});
					}
			
		};
		//btnAlert.setBackgroundResource(R.drawable.bell_normal);

		
	}
	
	private void turnOffDeviceButton() {
		this.btnDevice.setBackgroundResource(R.drawable.power_off);
	}

	private void turnOnDeviceButton() {
		this.btnDevice.setBackgroundResource(R.drawable.power_on);
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
//		getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(false);
//		getSherlockActivity().getSupportActionBar().setDisplayShowHomeEnabled(false);
//		getSherlockActivity().getSupportActionBar().setDisplayShowTitleEnabled(false);
		
		AlarmManager alarmManager = AlarmManager.sharedAlarmList(getActivity());
		
		if (alarmManager.isAlertAlarm()) {
			startAlertAnimation();
		} else {
			stopAlertAnimation();
		}
		
		if (alarmManager.isOnDevice()) {
			turnOnDeviceButton();
		} else {
			turnOffDeviceButton();
		}
		
		if (alarmManager.isUpdating()) {
			startUpdateAnimation();
		} else {
			stopUpdateAnimation();
		}
	}
	
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		// TODO Auto-generated method stub
		super.setUserVisibleHint(isVisibleToUser);
		
	
	}
	
	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		
		stopUpdateAnimation();
	}
}
